package org.apache.servicecomb.samples.practise.houserush.customer.manage.api;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.entity.Customer;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.entity.Qualification;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.POST;
import java.util.List;

@RestSchema(schemaId = ("customerApi"))
@RequestMapping("/")
public class CustomerManagerApiImpl implements CustomerManagerApi {
    @Autowired
    private CustomerService customerService;

    @Override
    //    GET 根据id查询客户 customers/{id}
    @GetMapping("customers/{id}")
    public Customer findCustomer(@PathVariable("id") int id) {
        return customerService.findCustomer(id);
    }

    @Override
    //    GET 查询所有客户 customers
    @GetMapping("customers")
    public List<Customer> indexCustomers() {
        return customerService.indexCustomers();
    }

    @Override
//        POST 创建客户 customers
    @PostMapping("customers")
    public Customer createCustomer(@RequestBody Customer customer) {
        return customerService.createCustomer(customer);
    }

    @Override
    //    PUT 根据id修改客户信息 customers/{id}
    @PutMapping("customers/{id}")
    public Customer updateCustomer(@PathVariable("id") int id,@RequestBody Customer customer) {
        customer.setId(id);
        List<Qualification> qualifications = customer.getQualifications();
        for (Qualification qualification : qualifications) {
            Customer c = new Customer();
            c.setId(id);
            qualification.setCustomer(c);
        }
        return customerService.updateCustomer(customer);
    }

    @Override
    //    PUT 根据id修改抢房资格 customers/{id}/update_qualifications
    @PutMapping("customers/{id}/update_qualifications")
    public Customer updateCustomerQualifications(@PathVariable("id") int id,@RequestBody List<Qualification> qualifications) {
        Customer customer = customerService.findCustomer(id);
        customerService.updateCustomerQualification(customer,qualifications);
        customer = customerService.findCustomer(id);
        return customer;
    }

    @Override
    //    DELETE 根据id删除客户 customers/{id}
    @DeleteMapping("customers/{id}")
    public void removeCustomer(int id) {
        customerService.removeCustomer(id);
    }
}
